import 'package:flutter/material.dart';

class BusinessDetailPageWidget extends StatefulWidget {
  BusinessDetailPageWidget({Key key}) : super(key: key);

  @override
  _BusinessDetailPageWidgetState createState() => _BusinessDetailPageWidgetState();
}

class _BusinessDetailPageWidgetState extends State<BusinessDetailPageWidget> {

  final GlobalKey<AnimatedListState> _listKey = new GlobalKey<AnimatedListState>();
  ListModel<int> _list;
  int _selectedItem;
  int _nextItem;

  @override
  void initState(){
    super.initState();
    _list = new ListModel<int>(
      listKey: _listKey,
      initialItems: <int>[0,1,2],
      removeItemBuilder: _buildRemoveItem
    );
    _nextItem = 3;
  }

  @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text('商家详情'),
        actions: [
          IconButton(icon: const Icon(Icons.favorite), onPressed: _favorite),
          IconButton(icon: const Icon(Icons.share), onPressed: _share)
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: AnimatedList(
          key: _listKey,
          initialItemCount: _list.length,
          itemBuilder: _buildItem,
        ),
      )
    );
  }

  Widget _buildItem(BuildContext context, int index, Animation<double> animation) {
    return CardItem(animation: animation, item: _list[index], selected: _selectedItem == _list[index],onTap: (){
      setState(() {
        _selectedItem = _selectedItem == _list[index] ? null : _list[index];
      });
    });
  }

  Widget _buildRemoveItem(int item, BuildContext context, Animation<double> animation){
    return CardItem(animation: animation, item: item, selected: false);
  }

  void _favorite(){

  }
  void _share(){
  }
}


class CardItem extends StatelessWidget{
  final Animation<double> animation;
  final VoidCallback onTap;
  final int item;
  final bool selected;

  const CardItem({
    Key key,
    @required this.animation,
    this.onTap,
    @required this.item,
    this.selected: false
  }) : assert(animation !=null),
        assert(item != null && item >= 0),
        assert(selected != null),
        super(key: key);

  Widget build(BuildContext context){
    TextStyle textStyle = Theme.of(context).textTheme.display1;
    if(selected)
      textStyle = textStyle.copyWith(color: Colors.lightGreenAccent[400]);

    return Padding(
      padding: const EdgeInsets.all(2),
      child: SizeTransition(
        axis: Axis.vertical,
        sizeFactor: animation,
        child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: onTap,
          child: SizedBox(
            height: 128.0,
            child: Card(
              color: Colors.primaries[item % Colors.primaries.length],
              child: Center(
                child: Text('Item $item'),
              ),
            ),
          ),
        ),
      ));
  }
}


class ListModel<E>{
  ListModel({
    @required this.listKey,
    @required this.removeItemBuilder,
    Iterable<E> initialItems,
  }) : assert(listKey != null),
        assert(removeItemBuilder != null),
        _items = new List<E>.from(initialItems ?? <E>[]);

  final GlobalKey<AnimatedListState> listKey;
  final dynamic removeItemBuilder;
  final List<E> _items;

  AnimatedListState get _animatedList => listKey.currentState;

  //  公开方法
  void insert(int index, E item) {
    _items.insert(index, item);
    _animatedList.insertItem(index);
  }

  E removeAt(int index){
    final E removeItem = _items.removeAt(index);
    if(removeItem != null){
      _animatedList.removeItem(index, (context, animation){
        return removeItemBuilder(removeItem,context,animation);
      });
    }
    return removeItem;
  }

  int get length => _items.length;
  E operator [](int index) => _items[index];
  int indexOf(E item)=> _items.indexOf(item);
}